Java Database Programming with JDBC Java Database Programming with JDBC
by Pratik Patel
Coriolis, The Coriolis Group
ISBN: 1576100561   Pub Date: 10/01/96
  

Previous Table of Contents Next


Listing 8.6 Reading the ICONSTORE table.

//————————————————————————————————————
// getIconDesc
// Read the IconStore ICONSTORE table and create a Hashtable
// a list of all the icons for the given category. The key is the
// icon containing description and the data value is the icon ID. The
// description is also added to the List object given.
//————————————————————————————————————
public Hashtable getIconDesc(
    Connection con,
    String category,
    List list)
{
    Hashtable table = new Hashtable();
    String desc;

    try {
        // Create a Statement object
         Statement stmt = con.createStatement();

        // Execute the query and process the results
         ResultSet rs = stmt.executeQuery(
                  "SELECT DESCRIPTION,ID FROM ICONSTORE WHERE CATEGORY=" +
                  
                  category);

        // Loop while more rows exist
          while (rs.next()) {
            desc = rs.getString(1);

           // Put the description and ID in the Hashtable
             table.put(desc, rs.getString(2));

           // Put the description in the list
            list.addItem(desc);

    }
    // Close the statement
      stmt.close();

}
catch (SQLException ex) {

          // An SQLException was generated. Dump the exception contents.
          // Note that there may be multiple SQLExceptions chained
          // together.
            System.out.println("\n*** SQLException caught ***\n");
           while (ex != null) {
            System.out.println("SQLState: " + ex.getSQLState());
           System.out.println("Message:  " + ex.getMessage());
            System.out.println("Vendor:   " + ex.getErrorCode());
            ex = ex.getNextException();
        }
        System.exit(1);
   }

   return table;
}

The process we used here is the same as we have seen before—creating a Statement, executing a query, processing the results, and closing the Statement. Listing 8.7 shows the entire code for the IconStore.init method. In addition to building the menu, we also build the CardLayout. It is important to note that the IconStore application is totally database-driven; no code will have to be modified to add or remove categories or images.

Listing 8.7 IconStore init method.

//————————————————————————————————————
// init
// Initialize the IconStore object. This includes reading the
// IconStore database for the icon descriptions.
//————————————————————————————————————
public void init()
{
    // Create our canvas that will be used to display the icons
    imageCanvas = new IconCanvas();

    // Establish a connection to the JDBC driver
     connection = establishConnection();

    // Get a Hashtable containing an entry for each icon category.
    // The key is the description and the data value is the
   // category number.
    categories = getCategories(connection);

    // Setup the menu bar
    menuBar = new MenuBar();

    // File menu
     fileMenu = new Menu("File");
     fileMenu.add(new MenuItem("Save As"));
      fileMenu.add(new MenuItem("Exit"));
     menuBar.add(fileMenu);

    // Icons menu
    sectionMenu = new Menu("Icons");

     // Setup our category lists, list panel (using a CardLayout), and
   // icon menu.
    iconListPanel = new Panel();
      iconListPanel.setLayout(new CardLayout());

    lists = new List[categories.size()];
   iconDesc = new Hashtable[categories.size()];
  Enumeration e = categories.keys();
 int listNo = 0;
 String desc;

     // Loop while there are more keys (category descriptions)
      while (e.hasMoreElements()) {
               desc = (String) e.nextElement();

        // The first item in the list will be our default
      if (listNo == 0) {
           currentList = desc;
     }

        // Create a new list, with a display size of 20
          lists[listNo] = new List(20, false);
   
        // Create a new CardLayout panel
             iconListPanel.add(desc, lists[listNo]);

        // Add the description to the Icons menu
        sectionMenu.add(new MenuItem(desc));

        // Get a Hashtable containing an entry for each row found
         // for this category. The key is the icon description and
         // the data value is the ID.

        iconDesc[listNo] = getIconDesc(connection,
                      (String) categories.get(desc), lists[listNo]);
     listNo++;
}
    // Add the Icons menu to the menu bar
     menuBar.add(sectionMenu);

    // Set the menu bar
    setMenuBar(menuBar);

    // Create a Save As file dialog box
     fileDialog = new FileDialog(this, "Save File", FileDialog.SAVE);

    // Setup our layout
     setLayout(new GridLayout(1,2));
     add(iconListPanel);
    add(imageCanvas);
}

It is very important to note how the CardLayout has been set up. Each of the lists is added to the CardLayout with a description as a title, which, in our case, is the name of the category. When the user selects a category from the Icons menu, we can use the category description to set the new CardLayout list. Figure 8.2 shows the initial screen after loading the database tables.


Figure 8.2  The IconStore main screen.


Previous Table of Contents Next